<?php

namespace Home\Controller;

use Api\Event\CacheEvent;

class ExploreController extends HomeController
{
    public function _initialize()
    {
        parent::_initialize();

        C('DEFAULT_THEME', 'v4');
        $this->currentTab = 'find';
    }

    public function index()
    {
        // 游戏关注排名写缓存
        $Redis = CacheEvent::instance(0);
        $key = 'IMBA.CACHE.WEB.EXPLORE.GAMES';
        $games = $Redis->get($key);
        if (empty($games)) {
            $game_ids = event('Game')->lists('', 'id');
            $game_ids = $game_ids ? array_column($game_ids, 'id') : [];
            $games = event('Game')->getGamesSortByScribe($game_ids);
            $Redis->setEx($key, 600, json_encode($games));
        } else {
            $games = json_decode($games, true);
        }
        unset($Redis);
        UID && $ids = event('GameScribe')->mine(UID);

        if (!empty($ids) && is_array($ids)) {
            // 按照用户关注排序
            $scribe_games = [];
            $unscribe_games = [];
            foreach ($games as $key => $value) {
                in_array($value['game_id'], $ids)
                ? $scribe_games[] = $value
                : $unscribe_games[] = $value;
            }
            $games = array_merge($scribe_games, $unscribe_games);
        }

        foreach ($games as &$game) {
            $game['info'] = event('Game')->info($game['game_id']);
            uri_format($game['info']['logo']);
            uri_format($game['info']['pc_explore']);
        }
        setRedirect();
        $this->games = $games;
        $this->title = '探索';
        $this->adCode = 'webexplorebanner';
        $this->display();
    }

    public function ajaxUsers()
    {
        $game_id = I('game_id', 0, 'intval');
        if (empty($game_id)) {
            $this->ajaxReturn(['state' => -2, 'info' => '参数错误']);
        }

        // 游戏活跃用户写缓存
        $Redis = CacheEvent::instance(0);
        $ukey = "IMBA.CACHE.WEB.EXPLORE.GAME_USER.{$game_id}";
        $users = $Redis->get($ukey);
        if (empty($users)) {
            $exclude_uids = array_map('intval', C('EXCLUDE_USER_IDS', null, []));
            $map = [
                'game_id' => $game_id,
                'type'    => 0,
                'status'  => 1,
                'race_id' => ['$eq' => null],
                'uid'     => ['$nin' => $exclude_uids],
            ];
            $users = event('Video')->getTopUsers($map, 7, true);
            $Redis->setEx($ukey, 600, json_encode($users));
        } else {
            $users = json_decode($users, true);
        }
        unset($Redis);
        $users = $users ? array_slice($users, 0, 4) : [];

        $this->data = $users;
        C('DEFAULT_THEME', 'v4');
        $html = $this->fetch('user_template');
        $this->ajaxReturn(['state' => 0, 'html' => $html]);
    }

    public function ajaxVideos()
    {
        $game_id = I('game_id', 0, 'intval');
        if (empty($game_id)) {
            $this->ajaxReturn(['state' => -2, 'info' => '参数错误']);
        }

        // 推荐视频写缓存
        $Redis = CacheEvent::instance(0);
        $vkey = "IMBA.CACHE.WEB.EXPLORE.GAME_VIDEO.{$game_id}";
        $videos = $Redis->get($vkey);
        if (empty($videos)) {
            $vids = event('Video')->getByTopicAndGame(3, [$game_id], 0, 6);
            $filterMap = ['id','title','thumb','pre_uri','uri', 'user', 'dpi_levels', 'detail','create_time','game_id','game','type','icons', 'tags', 'type', 'status', 'dpi_height', 'dpi_width', 'statis'];
            $videos = $this->lists_format($vids, $filterMap);
            $Redis->setEx($vkey, 600, json_encode($videos));
        } else {
            $videos = json_decode($videos, true);
        }
        unset($Redis);
        $this->data = $videos;
        C('DEFAULT_THEME', 'v4');
        $html = $this->fetch('video_template');
        $this->ajaxReturn(['state' => 0, 'data' => $videos, 'html' => $html]);
    }
}